﻿using FlyingHat.Model.Models;
using FlyingHat.Model.Models.Global;
using FlyingHat.Service.IService;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace FlyingHat.Web.Controllers
{
    /// <summary>
    /// 图书
    /// </summary>
    public class BookController : BaseController
    {
        private readonly IBookService bookService;
        private readonly ILibraryService libraryService;
        private readonly IBookshelfService bookshelfService;

        public BookController(IBookService bookService, ILibraryService libraryService, IBookshelfService bookshelfService)
        {
            this.bookService = bookService;
            this.libraryService = libraryService;
            this.bookshelfService = bookshelfService;
        }

        [HttpGet]
        public async Task<IActionResult> Index()
        {
            var sel = await libraryService.GetSelect();
            sel.Insert(0, new SelectModel { Text = "请选择", Value = "0" });
            ViewBag.SelectLibrary = sel.Select(i => new SelectListItem { Text = i.Text, Value = i.Value });
            return View();
        }

        [HttpGet]
        public async Task<IActionResult> GetList(string? name, string? author, long? libraryId, long? bookselfId, int page, int limit)
        {
            var res = await bookService.GetList(name, author, libraryId, bookselfId, page, limit);
            return Json(res);
        }

        /// <summary>
        /// 获取书架
        /// </summary>
        /// <param name="libraryId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetBookshelf(long libraryId)
        {
            var sel = await bookshelfService.GetSelect(libraryId);
            sel.Insert(0, new SelectModel { Text = "请选择书架", Value = "0" });
            return Json(sel);
        }

        [HttpGet]
        public async Task<IActionResult> Add()
        {
            var sel = await libraryService.GetSelect();
            sel.Insert(0, new SelectModel { Text = "请选择图书室", Value = "0" });
            ViewBag.SelectLibrary = sel.Select(i => new SelectListItem { Text = i.Text, Value = i.Value });
            return View();
        }

        [HttpPost]
        public async Task<IActionResult> AddOrUpdate(BookKeyModel model)
             => Json(await bookService.AddOrUpdate(model).ConfigureAwait(false));
    }
}
